<template>
	<el-container class="home-container">
		<!-- 头部区域 -->
		<el-header>
			<div class="logo">
				<img src="../assets/logoGuet.png" alt="">
				<h3>电商后台管理系统</h3>
			</div>
			<el-button type="info" @click="logout" round>退出</el-button>
		</el-header>
		<!-- 页面主体区 -->
		<el-container>
			<!-- 侧边栏 -->
			<el-aside :style=" isCollapse?'width:64px':'width:200px'">
				<div class="toggle-button" @click="toggleCollapse">|||</div>
				<!-- 侧边栏菜单区 element-ui提供的 -->
				<!--el-menu提供的属性 unique-opened  collapse collapse-transition -->
				<el-menu text-color="#fff" active-text-color="#409eFF" background-color="#333744" unique-opened
					:collapse="isCollapse" :collapse-transition="false" router :default-active="$route.path">
					<!-- router开启路由模式，页面跳转 default-active=""高亮显示 -->
					<!-- 一级菜单   因为index值接收字符串，所以item.id + '' 隐式转换成字符串 -->
					<el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
						<!-- 一级菜单的模板区 -->
						<template slot="title">
							<!-- 图标 -->
							<i :class="icons[item.id]"></i>
							<!-- 文本 -->
							<span>{{item.authName}}</span>
						</template>

						<!-- 二级菜单 -->
						<!-- 跳转页面:index="'/' + subItem.path" -->
						<el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
							<template slot="title">
								<!-- 图标 -->
								<i class="el-icon-menu"></i>
								<!-- 文本 -->
								<span>{{subItem.authName}}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 右侧内容主体区 -->
			<el-main>
				<!-- 路由占位符  welcome页面-->
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		data() {
			return {
				// 左侧菜单数据
				menuList: [],
				// 图标
				icons: {
					'125': 'el-icon-s-custom',
					'103': 'el-icon-s-tools',
					'101': 'el-icon-s-goods',
					'102': 'el-icon-s-claim',
					'145': 'el-icon-s-data'
				},
				// 是否折叠
				isCollapse: false
			}
		},
		created() { //生命周期值函数
			this.getMenuList()
		},
		methods: {
			logout() {
				//清空token
				window.sessionStorage.clear();
				//跳转到登录页
				this.$router.push('/login');
			},
			// 获取所有的菜单
			async getMenuList() {
				const {
					data: res
				} = await this.$http.get('menus')
				// console.log(res)
				if (res.meta.status !== 200) {
					return this.$message.error(res.meta.msg)
				}
				this.menuList = res.data
			},
			// 点击按钮 切换菜单的折叠展开
			toggleCollapse() {
				this.isCollapse = !this.isCollapse;
			}
		}
	};
</script>

<style Lang="less" scoped="scoped">
	.home-container {
		height: 100%;
	}

	.el-header {
		background-color: #373D41;
		display: flex;
		justify-content: space-between;	/*左右对齐*/
		padding-left: 0;/*把图片*/
		align-items: center;		/*设置按钮居中*/
		color: #fff;
		font-size: 20px;
	}

	.logo {
		display: flex;
		align-items: center;	/*纵向对齐*/
	}

	h3 {
		margin-left: 20px;
	}

	.el-aside {
		background-color: #333744;

	}

	.el-menu {
		border-right: 0;
	}

	.el-main {
		background-color: #eaedf1;
	}

	.iconfont {}

	/* 侧边栏收缩框 */
	.toggle-button {
		background-color: #4a5064;
		font-size: 10px;
		color: #fff;
		line-height: 24px;
		text-align: center;
		letter-spacing: 0.2em;		/*字间距*/
		cursor: pointer;
	}
</style>
